package com.mauroleon.promotail.mobile;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Application;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.util.Log;

import com.mauroleon.promotail.canonical.Categoria;
import com.mauroleon.promotail.canonical.Promocion;
import com.mauroleon.promotail.service.PromotailService;
import com.mauroleon.promotail.service.PromotailServiceImpl;

public class PromotailApp extends Application {

	private ConnectivityManager cMgr;
	private PromotailService promotailService;
	private List<Categoria> categoriaList;
	private Map<Long, Bitmap> imageCache;
	private Promocion currentItem;

	public ConnectivityManager getcMgr() {
		return cMgr;
	}

	public void setcMgr(ConnectivityManager cMgr) {
		this.cMgr = cMgr;
	}

	public PromotailService getPromotailService() {
		return promotailService;
	}

	public void setPromotailService(PromotailService promotailService) {
		this.promotailService = promotailService;
	}

	public List<Categoria> getCategoriaList() {
		return categoriaList;
	}

	public void setCategoriaList(List<Categoria> categoriaList) {
		this.categoriaList = categoriaList;
	}

	public Map<Long, Bitmap> getImageCache() {
		return imageCache;
	}

	public void setImageCache(Map<Long, Bitmap> imageCache) {
		this.imageCache = imageCache;
	}

	public Promocion getCurrentItem() {
		return currentItem;
	}

	public void setCurrentItem(Promocion currentItem) {
		this.currentItem = currentItem;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		this.cMgr = (ConnectivityManager) this
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		this.promotailService = new PromotailServiceImpl();
		this.categoriaList = new ArrayList<Categoria>(6);
		this.imageCache = new HashMap<Long, Bitmap>();
	}

	@Override
	public void onTerminate() {
		// not guaranteed to be called
		super.onTerminate();
	}

	//
	// helper methods (used by more than one other activity, so placed here,
	// could be util class too)
	//
	public Bitmap retrieveBitmap(String urlString) {
		Log.d(Constantes.LOG_TAG, "making HTTP trip for image:" + urlString);
		Bitmap bitmap = null;
		try {
			URL url = new URL(urlString);
			// NOTE, be careful about just doing "url.openStream()"
			// it's a shortcut for openConnection().getInputStream() and doesn't
			// set timeouts
			// (the defaults are "infinite" so it will wait forever if endpoint
			// server is down)
			// do it properly with a few more lines of code . . .
			URLConnection conn = url.openConnection();
			conn.setConnectTimeout(3000);
			conn.setReadTimeout(5000);
			bitmap = BitmapFactory.decodeStream(conn.getInputStream());
		} catch (MalformedURLException e) {
			Log.e(Constantes.LOG_TAG, "Exception loading image, malformed URL",
					e);
		} catch (IOException e) {
			Log.e(Constantes.LOG_TAG, "Exception loading image, IO error", e);
		}
		return bitmap;
	}

	public boolean connectionPresent() {
		NetworkInfo netInfo = cMgr.getActiveNetworkInfo();
		if ((netInfo != null) && (netInfo.getState() != null)) {
			return netInfo.getState().equals(State.CONNECTED);
		}
		return false;
	}

}
